package com.example.controller;

import com.example.entity.Notice;
import com.example.entity.User;
import com.example.entity.vo.NoticeVO;
import com.example.security.SecurityUser;
import com.example.service.NoticeService;
import com.example.service.UserService;
import com.example.util.CustomUtil;
import com.example.util.ResultEntity;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    NoticeService noticeService;

    @Autowired
    UserService userService;

    public NoticeVO transformToNoticeVO(Notice notice) {
        User user = userService.getById(notice.getUserid());
        NoticeVO noticeVO = new NoticeVO();
        BeanUtils.copyProperties(notice, noticeVO);
        noticeVO.setUsername(user.getUsername());
        return noticeVO;
    }

    @ResponseBody
    @RequestMapping("/get/all/notices.json")
    public ResultEntity<List<NoticeVO>> getAllNotices(@RequestParam("page") Integer page,
                                                      @RequestParam("limit") Integer limit,
                                                      @RequestParam(value = "name", required = false) String name,
                                                      @RequestParam(value = "title", required = false) String title,
                                                      @RequestParam(value = "username", required = false) String username
    ) {
        if (name != null) {
            if (name.isEmpty()) {
                name = null;
            } else {
                name = '%' + name + '%';
            }
        }
        if (title != null) {
            if (title.isEmpty()) {
                title = null;
            } else {
                title = '%' + title + '%';
            }
        }
        if (username != null) {
            if (username.isEmpty()) {
                username = null;
            } else {
                username = '%' + username + '%';
            }
        }

        List<Notice> allNotices = noticeService.getByExampleWithLimit(name, title, username, page, limit);
        List<NoticeVO> allNoticeVOs = new ArrayList<>(allNotices.size());
        for (Notice notice : allNotices) {
            NoticeVO noticeVO = transformToNoticeVO(notice);
            allNoticeVOs.add(noticeVO);
        }
        int count = noticeService.getCountByExample(name, title, username);
        return ResultEntity.createResultEntity(0, null, count, allNoticeVOs);
    }

    @PreAuthorize("hasRole('ROLE_2') or hasRole('ROLE_3') ")
    @ResponseBody
    @RequestMapping("/remove")
    public ResultEntity<Object> removeById(@Param("id") Integer id) {
        try {
            noticeService.removeById(id);
        } catch (Exception e) {
            return ResultEntity.createResultEntity(-1, e.getMessage(), 0, null);
        }
        return ResultEntity.createResultEntity(0, null, 0, null);
    }

    @PreAuthorize("hasRole('ROLE_2') or hasRole('ROLE_3') ")
    @ResponseBody
    @RequestMapping("/update")
    public ResultEntity<Object> updateById(@RequestBody NoticeVO noticeVO) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeVO, notice);
        try {
            noticeService.updateSelectiveById(notice);
        } catch (Exception e) {
            return ResultEntity.createResultEntity(-1, e.getMessage(), 0, null);
        }
        return ResultEntity.createResultEntity(0, null, 0, null);
    }

    @GetMapping("/read/page.html")
    public String readPage(@Param("id") Integer id, ModelMap modelMap) {
        Notice notice = noticeService.getById(id);
        NoticeVO noticeVO = transformToNoticeVO(notice);
        modelMap.addAttribute("noticeVO", noticeVO);
        return "include/notice-read-page";
    }

    @PreAuthorize("hasRole('ROLE_2') or hasRole('ROLE_3') ")
    @RequestMapping("/add/page.html")
    public String addPage() {
        return "include/add-notice";
    }

    @PreAuthorize("hasRole('ROLE_2') or hasRole('ROLE_3') ")
    @ResponseBody
    @RequestMapping("/add")
    public ResultEntity<Object> addNotice(@RequestBody Notice notice, HttpSession session) {
        SecurityUser securityUser = CustomUtil.currentUser(session);
        User user = securityUser.getOriginalUser();
        notice.setUserid(user.getId());
        try {
            noticeService.addNotice(notice);
        } catch (Exception e) {
            return ResultEntity.createResultEntity(-1, e.getMessage(), 0, null);
        }
        return ResultEntity.createResultEntity(0, null, 0, null);
    }
}
